package com.itheima.dao.impl;

import com.itheima.pojo.Account;
import com.itheima.dao.AccountDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private QueryRunner qr;

    public void add(Account account) throws SQLException {
        String sql = "insert into account values(null,?,?)";
        qr.update(sql, account.getName(), account.getMoney());
    }

    public void delete(int id) throws SQLException {
        String sql = "delete from account where id = ?";
        qr.update(sql, id);
    }

    public void update(Account account) throws SQLException {
        String sql = "update account set name = ? and money = ?";
        qr.update(sql, account.getName(), account.getMoney());
    }

    public List<Account> findAll() throws SQLException {
        String sql = "select * from account";
        return qr.query(sql,new BeanListHandler<Account>(Account.class));
    }
}
